package com.codefactory.generator.method;

import com.codefactory.generator.utils.CaseUtil;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

import java.util.List;

public class VarNameMethod implements TemplateMethodModelEx {
    @Override
    public Object exec(List arguments) throws TemplateModelException {
        if (arguments.size() != 1) {
            throw new TemplateModelException("Invalid argument count, expected 1, got " + arguments.size());
        }
        if (!(arguments.get(0) instanceof SimpleScalar)) {
            throw new TemplateModelException("Invalid argument type, expected SimpleScalar");
        }
        SimpleScalar scalar = (SimpleScalar) arguments.get(0);
        return new SimpleScalar(CaseUtil.tableNameToVarName(scalar.getAsString()));
    }
}